<script language="JavaScript">
	function fncMinus()
	{
		 if(isNaN(document.frmsale.txtinputMoney.value) || document.frmsale.txtinputMoney.value == "")
		 {
			alert('Please input Number only.');
			document.frmsale.txtinputMoney.focus();
			return;
		 }
		 var inputMoney=parseFloat(document.frmsale.txtinputMoney.value);
		 var subTotal=parseFloat(document.frmsale.subtotal.value);
		 
		 if(inputMoney >= subTotal){
			 document.frmsale.balance.value=00;
			 document.frmsale.txtleftMoney.value = inputMoney - subTotal;
			 document.frmsale.payment.value = subTotal;
		 }else if(inputMoney < subTotal){
			 document.frmsale.payment.value = inputMoney;
			 document.frmsale.balance.value = subTotal - inputMoney;
			 document.frmsale.txtleftMoney.value=0;
		 }
	}
	function redirect(url){
    	window.location = url;
	}
	/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}
/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	textBox.value = trim(textBox.value);
}
/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}
//expense new
function checkPayment()
{
	with (window.document.frmsale) {
		if (isEmpty(txtinputMoney, 'Please input payment...!')) {
			return false;
		}/*else if (isEmpty(cboaccount, 'Please select account...!')) {
			return false;
		} 
		}*/
	}
}
</script>
<?php
	@$strMode = $_POST["tMode"];
	@$strID = $_POST["tpd_name"];
	
	$strSQL = "SELECT * FROM tbl_carts";
	$objQuery = mysql_query($strSQL);
	$sql_cart = mysql_query($strSQL)
?>
<form name="frmsale" id="frmsale" method="post" action="sale_verify_user.php?control=insert" onsubmit="return checkPayment();">
   <table width="100%" class="bordered">
   		<tr>
        	<th><img src="images/shopping_card.png"  /> Your Products to sell now...!</th>
        </tr>
   </table>
    <table width="100%" class="bordered">
        <tr>
        	<?php 
				$maxOfAll=maxOfAll("id","tbl_sales");
				$maxOfAll=$maxOfAll + 1;
				$saleNumber="SA" .$maxOfAll ;
			?>  
          <td width="15%">Sale ID<input name="salenumber" type="text" id="saleNumber" readonly value="<?php echo @$saleNumber; ?>" style="width:50px;"> </td>
           <?php 
			 $maxOfAll=maxOfAll("id","tbl_sales");
			 $maxOfAll=$maxOfAll + 1;
			 $billNumber="B" .$maxOfAll ;
		   ?>
          <td width="15%">Bill No <input type="text" name="billnumber" style="width:60px;" readonly value="<?php echo @$billNumber;?>" id="billnumber" ></td>
          <td width="32">Date<input type="text" id="date" name="date" class="date_input" value="<?php echo date('d-m-Y');?>" style="width:70px;"></td>
          <td width="120" colspan="2">Customer
          		<select name="customer">
                   <option value=""><--Please customer--></option>
                   <?php
						@$cusName=$_REQUEST['msgStatus'];
                        $sql="SELECT * FROM tbl_customers ORDER BY cus_id DESC";
                        $result=query($sql);
                        while($row=fetch_object($result))
                        {
							if(@$cusName == $row->cus_name)
								$selected = " selected='selected'";
							else	
								$selected = "";
                   ?>
                   <option value="<?php echo $row->cus_name;?>"<?php echo $selected;?>><?php echo $row->cus_name;?></option>
                   <?php 
                       }
                   ?>
              </select>
          </td>
          <td>
          		<div class="new"><a href="index.php?page=customer_list&action=customer_new_sale">New</a></div>
          </td>
        </tr>
      <tr>
        <th > <div align="center">Product</div></th>
        <th > <div align="center">Qty</div></th>
        <th > <div align="center">Buy Rate</div></th>
        <th > <div align="center">Sell Rate</div></th>
        <th > <div align="center">Avail</div></th>
        <th> <div align="center">Total</div></th>
      </tr>
    <?php
	$row_cart = mysql_fetch_array($sql_cart);
	if($row_cart['pd_name']==""){
		//pageRedirect("AjaxPHPInsertRecord1.php?","Please Add product to your carts...!");
		pageRedirect("index_user.php?page=sale_list&action=sales_user&","You can not check out, Carts is empty...!");
	}else{
		while($objResult = mysql_fetch_array($objQuery))
		{
		?>
		  <tr>
			<td><div align="center"><?php echo $objResult["pd_name"];?></div> <input type="hidden" name="name[]" id="name" value="<?php echo $objResult["pd_name"];?>" size="20"   /></td>
			<td><?php echo $objResult["qty"];?><input type="hidden" name="quantity[]" id="quantity" value="<?php echo $objResult["qty"];?>" size="20" /></td>
			<td><?php echo $objResult["buying_price"];?><input type="hidden" name="brate[]" id="brate" value="<?php echo $objResult["buying_price"];?>" size="20"   /></td>
			<td><?php echo $objResult["selling_price"];?><input type="hidden" name="srate[]" id="srate" value="<?php echo $objResult["selling_price"];?>" size="20"   /></td>
			<td><?php echo $objResult["avail"];?><input type="hidden" name="avail[]" id="avail" value="<?php echo $objResult["avail"];?>" size="20"   /></td>
			<td align="right"><?php echo $objResult["total"];?><input type="hidden" name="total[]" id="total" value="<?php echo $objResult["total"];?>" size="20"   /></td>
		  </tr>
		<?php
		}
		?>
			<tr>
				<th >Payment</th>
				<th >Balance</th>
				<th width="20%">Left Money</th>
				<th >Mode</th>
				<th>Next Due</th>
				<th >Sub Total</th>
		  </tr>
          
          <tr>
				<td align="right"><input type="text" class="input req-min" minlength="1" name="txtinputMoney" style="width:70px; " id="txtinputMoney"   onKeyUp="JavaScript:fncMinus();">
				<input type="hidden"  name="payment" style="width:100px; " id="payment" >
				</td>
				<td align="right"><input name="balance" type="text"  id="balance" style="width:70px;" value="0.00" readonly></td>
				<td align="right"><input type="text" id="txtleftMoney" name="txtleftMoney"  value="0.00" style="width:70px; " readonly="readonly" ></td>
				<td align="right">
					<select name="mode">
					   <option value="cheque">Cheque</option>
					   <option value="cash" selected>Cash</option>
					   <option value="others">others</option>
					</select>

				</td>
				<td>
                	<input type="text" id="lastupdate" name="lastupdate" class="date_input" value="<?php echo date('d-m-Y');?>" style="width:100px; ">
				</td>
				<td >
                	<?php 
						$sql_sum=" SELECT SUM(total) AS subTotal FROM tbl_carts";
						$result_sum=mysql_query($sql_sum);
						$row_sum=mysql_fetch_assoc($result_sum);
					?>
					<input name="subtotal" id="subtotal" type="text" value="<?php echo $row_sum['subTotal'];?>" readonly style="width:70px; text-align:right; color:#333333; font-weight:bold; font-size:16px;">
					
				</td>
		  </tr>
          <tr>
          		<td colspan="6">
                	<input class="btnSubmit" type="button" name="btnshopping" value="Go Shopping" onclick="redirect('index.php?page=sale_list&action=sales')" >
					<input class="btnSubmit" type="submit" name="btnsubmit" value="Save"  style="width:80px;" >
                </td>
          </tr>
		</table>
</form>
<?php
	}//end check cart empty
?>